﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UtilLib.SQL
{
    public class SQLHelper
    {
        private static WebRef wr = new WebRef();
        private static WebReference.MyService service = new WebReference.MyService();
        private static WebReference.WebSlected[] sd = new WebReference.WebSlected[0];

        private static byte[] ids;
        private static DataSet ds;


        public SQLHelper()
        {
            getIDS();
        }
        private static void getIDS()
        {
            DataTable idsdt = new DataTable();
            idsdt.TableName = "635986646815214902";
            ids = wr.GetBinaryFormatData(idsdt);
        }


        public static DataTable select(string tableName, string asTableName, string xtRfa, string[] selectFields, string[] selectAss, object[] wheres)
        {
            // string[] selectFields = "id,ts_sx_id,ts_fa_id".Split(',');
            // string[] selectAss = new string[] { "id", "ts_sx_id", "ts_fa_id",};
            // object[] wheres = new object[] {asTableName,"(","id","等于",[value],")","或者"};
            ds = new DataSet();
            try
            {
                if(selectAss==null)
                {
                    selectAss = "".Split();
                }
                wr.DBMCADD(new object[] { asTableName, tableName, xtRfa == "xt" ? "XTBM" : "FABM", "select", "" }, ds);
                wr.selectAdd(asTableName, selectFields, selectAss, ds);
                wr.whereAdd(wheres, ds);
                sd = wr.DataSetToSlecteds(ds);
                sd = service.request_select(sd, ids);
                ds = wr.SlectedsToDataSet(sd);
                if (ds.Tables.Count > 0 && ds.Tables[0].TableName.ToUpper() != "ERROR")
                {
                    return ds.Tables[0];
                }
                throw new Exception("wrong in select SQL");
            }
            catch (Exception)
            {

            }
            return null;
        }
        public static bool insert(string tableName, string asTableName, string[] insertFields, string[] insertValues)
        {
            // string[] insertFields = "id:pk,ts_sx_id,ts_fa_id".Split(',');
            // string[] insertValues = new string[] { id, "", "",};              

            ds = new DataSet();
            try
            {
                wr.DBMCADD(new object[] { asTableName, tableName, "FABM", "insert", "" }, ds);
                wr.insertAdd(insertFields, insertValues, ds, asTableName);
                sd = wr.DataSetToSlecteds(ds);
                sd = service.request_select(sd, ids);
                ds = wr.SlectedsToDataSet(sd);
                if (ds.Tables.Count > 0 && ds.Tables[0].TableName.ToUpper() != "ERROR")
                {
                    return true;
                }
                throw new Exception("wrong in insert SQL");
            }
            catch (Exception)
            {

            }
            return false;
        }
        public static bool delete(string tableName, string asTableName, object[] wheres)
        {
            // object[] wherezdm = new object[] { asTableName, "", "id", "等于", [id], "", "并且" };
            ds = new DataSet();
            try
            {
                wr.DBMCADD(new object[] { asTableName, tableName, "FABM", "delete", "" }, ds);
                wr.whereAdd(wheres, ds);
                sd = wr.DataSetToSlecteds(ds);
                sd = service.request_select(sd, ids);
                ds = wr.SlectedsToDataSet(sd);
                if (ds.Tables.Count > 0 && ds.Tables[0].TableName.ToUpper() != "ERROR")
                {
                    return true;
                }
                throw new Exception("wrong in delete SQL");
            }
            catch (Exception)
            {

            }
            return false;
        }
        public static bool update(string tableName, string asTableName, string[] updateFields, object[] updateValues, object[] wheres)
        {
            // string[] updateFields = "id".Split(',');
            // string[] updateValues = new object[] { [id] };
            // string[] wheres = new object[] { asTableName , "", "id", "等于", [id], "", "并且" }
            ds = new DataSet();
            try
            {
                wr.DBMCADD(new object[] { asTableName, tableName, "FABM", "update", "" }, ds);
                wr.updateAdd("fn", updateFields, "=", updateValues, ds);
                wr.whereAdd(wheres, ds);
                sd = wr.DataSetToSlecteds(ds);
                sd = service.request_select(sd, ids);
                ds = wr.SlectedsToDataSet(sd);
                if (ds.Tables.Count > 0 && ds.Tables[0].TableName.ToUpper() != "ERROR")
                {
                    return true;
                }
                throw new Exception("wrong in update SQL");
            }
            catch (Exception)
            {

            }
            return false;
        }
    }
}
